/**
 * Created by wyy on 17-7-11.
 */
angular.module('headBar', [])
    .component('headBar', {
        templateUrl: 'component/base/headBar/head_bar.template.html',
        controller: ['$rootScope', '$scope', '$stateParams', 'Api', 'Enum', '$state', 'RentProtocolModel', function ($rootScope, $scope,$stateParams, Api, Enum, $state, RentProtocolModel) {


            setupNavSelectedWithState($state.current.name);

            $scope.apartmentId = $scope.$parent.apartmentId;
            $scope.barItems = Enum.HeadBarinformationItems;
            setupNavSelectWhenStateChange();

            Api.getCompanyDetail(null, function (result) {
                $scope.apartmentId = $stateParams.apartmentId;
                Api.getApartmentListInfo(result.data.communityId, $scope.apartmentId, function (apartmentInfo) {
                    $rootScope.apamentInfo = apartmentInfo.data
                });
            });

            $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
                if ($rootScope.apamentInfo) {
                    initNavDisable($rootScope.apamentInfo.status);
                }
            });

            $rootScope.$watch('apamentInfo', function (apamentInfo) {
                if (apamentInfo) {
                    initNavDisable($rootScope.apamentInfo.status);
                }
            });


            function initNavDisable(status) {
                var $headbar = $('.headbar');
                if (status == 'FREE') {
                    $headbar.find('button').attr('disabled', 'disabled');
                    $headbar.find('button[name=apartmentinfoNav]').removeAttr('disabled');
                    $headbar.find('button[name=smart_equipment_detailNav]').removeAttr('disabled');

                } else if (status == 'OFFLINE') {
                    $headbar.find('button').attr('disabled', 'disabled');
                    $headbar.find('button[name=apartmentinfoNav]').removeAttr('disabled');
                    $headbar.find('button[name=smart_equipment_detailNav]').removeAttr('disabled');
                    // console.log('button[name=smart_equipment_detailNav]')

                } else if (status == 'CHECK') {
                    $headbar.find('button').removeAttr('disabled');
                }
                var $navButton = $('.nav-button');
                $navButton.find('button').removeClass('active');
                $navButton.find('button[name=' + status + ']').addClass('active')


            }

            function setupNavSelectWhenStateChange() {
                /* 获取路由变化 */
                $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
                    setupNavSelectedWithState(toState.name);
                });
            }


            function setupNavSelectedWithState(stateName) {
                if (stateName.indexOf('company.companyApartmentInfo') !== -1) {
                    $scope.navSelect = Enum.HeadBarinformationItems[0];
                }
                else if (stateName.indexOf('company.companyRentagreement') !== -1) {
                    $scope.navSelect = Enum.HeadBarinformationItems[1];
                }
                else if (stateName.indexOf('company.companyRentpayment') !== -1) {
                    $scope.navSelect = Enum.HeadBarinformationItems[2];
                }
                else if (stateName.indexOf('company.companySmartEquipment') !== -1) {
                    $scope.navSelect = Enum.HeadBarinformationItems[3];
                }
            }


                $scope.selectHead = function (item) {
                    // setupRentinfo();

                $scope.navSelect = item;
                location.href = item.jumpUrl + $rootScope.apamentInfo.id;
            };

//apartmentinfo rent_agreement rent_payment smart_equipment

            // getRentlistInfo()
            // function getRentlistInfo() {
            //     Api.getRentlistInfo($scope.apartmentId,function (result) {
            //         $rootScope.rentinfoModels=result.data;
            //     });
            // }
            // var locationUrl='login.html#';
            // function setupRentinfo() {
            //     $rootScope.$watch('rentinfoModels',function (rentinfo) {
            //         console.log(rentinfo)
            //         if (rentinfo !== null){
            //             $scope.barItems[1].jumpUrl=locationUrl+'company/rent_agreement_detail/';
            //         }
            //     });
            //
            // }
        }]
    });
